#include "robot.h"
#include "strategy.h"
#include "robotgraplord.h"
#include "robotplayhand.h"
#include "datamanager.h"
#include "taskqueue.h"
#include <QDebug>
#include <QThreadPool>

Robot::Robot(QObject *parent) : Player{parent}
{
    m_type = Player::Robot;
}

// 准备抢地主
void Robot::prepareCallLord()
{
    RobotGrapLord* task = new RobotGrapLord(this);
    QThreadPool::globalInstance()->start(task);
    /*
    connect(subThread, &RobotGrapLord::finished, this, [=]() {
        qDebug() << "RobotGrapLord子线程对象被析构......" << ", Robot name: " << this->getName();
        subThread->deleteLater();
    });
    subThread->start();
    */
}

// 准备出牌
void Robot::preparePlayHand()
{
    RobotPlayHand* task = new RobotPlayHand(this);
    QThreadPool::globalInstance()->start(task);
    /*
    connect(subThread, &RobotGrapLord::finished, this, [=]() {
        qDebug() << "RobotGrapLord子线程对象被析构......" << ", Robot name: " << this->getName();
        subThread->deleteLater();
    });
    subThread->start();
    */
}

// 考虑叫地主
void Robot::thinkCallLord()
{
    if (DataManager::getInstance()->isNetworkMode()) {
        Task t = TaskQueue::getInstance()->take();
        grabLordBet(t.bet);
        return ;
    }

   /*
    * 基于手牌的好坏选择是否叫地主，为每种牌型设置不同的权重，基于权重来考虑下注多少分
    *  大小王: 6
    *  炸弹/顺子: 5
    *  三张点数点数相同的牌: 4
    *  2的权重: 3
    *  对子: 1
   */

    int weight = 0;
    Strategy st(this, m_cards);
    weight += st.getRangeCards(Card::CardPoint::Card_SJ, Card::CardPoint::Card_BJ).cardCount() * 6;

    QVector<Cards> optSeq = st.pickOptimalSeqSingles();
    weight += optSeq.size() * 5;

    QVector<Cards> bombs = st.findCardsByCount(4);
    weight += bombs.size() * 5;

    weight += m_cards.pointCount(Card::CardPoint::Card_2) * 3;

    Cards tmp = m_cards;
    tmp.remove(optSeq);
    tmp.remove(bombs);
    Cards card2 = st.getRangeCards(Card::CardPoint::Card_2, Card::CardPoint::Card_2);
    tmp.remove(card2);
    QVector<Cards> triples = Strategy(this, tmp).findCardsByCount(3);
    weight += triples.size() * 4;

    tmp.remove(triples);
    QVector<Cards> pairs = Strategy(this, tmp).findCardsByCount(2);
    weight += pairs.size() * 1;

    if (weight >= 22) {
        grabLordBet(3);
    } else if (weight < 22 && weight >= 18) {
        grabLordBet(2);
    } else if (weight < 18 && weight >= 10) {
        grabLordBet(1);
    } else {
        grabLordBet(0);
    }
}

void Robot::thinkPlayHand()
{
    if (DataManager::getInstance()->isNetworkMode()) {
        Task t = TaskQueue::getInstance()->take();
        playHand(t.cs);
        return ;
    }
    Strategy st(this, m_cards);
    Cards cs = st.makeStrategy();
    playHand(cs); // 调用出牌函数

}
